package com.hqd.ch03.v51.aop.framework.adapter;

import com.hqd.ch03.v51.aop.AfterAdvice;
import com.hqd.ch03.v51.aop.AfterReturningAdvice;
import com.hqd.ch03.v51.aopalliance.intercept.MethodInterceptor;
import com.hqd.ch03.v51.aopalliance.intercept.MethodInvocation;
import lombok.AllArgsConstructor;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

@AllArgsConstructor
public class AfterReturningAdviceInterceptor implements MethodInterceptor, AfterAdvice {
    private final AfterReturningAdvice advice;

    @Nullable
    @Override
    public Object invoke(@Nonnull MethodInvocation mi) throws Throwable {
        Object result = mi.proceed();
        advice.afterReturning(result, mi.getMethod(), mi.getArguments(), mi.getThis());
        return result;
    }
}
